package player.ui.browse;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import player.PS;
import player.Tools;
import player.mplayer.MediaInfo;
import player.ui.playlist.PlayListManage;
import player.ui.playlist.PlayListModel;

public class ImgButtonItem extends JComponent{

	private static final long serialVersionUID = -5789070731491186856L;

	BufferedImage img;
	float rate=1;
	float fh=0f,fw=0f;
	int imgWidth=1,imgHeight=1;
	boolean mouseOver=false;
	File[] imgFiles;
	Thread imgChangeThread;
	int si=1;
	String mediaPath;
	PlayListManage pm;
	PlayListModel plm;
	MediaInfo media;
	ImgButtonItem ImgButtonItem;
	int index;
	float cx, cy = 0, cw, ch;
	boolean threadRun=false;
	public ImgButtonItem(PlayListManage pm,final PlayListModel plm,
			final MediaInfo media,final int index,String text,File[] imgFiles) {
		super();
		this.index=index;
		ImgButtonItem=this;
		this.plm=plm;
		this.pm=pm;
		this.media=media;
		this.imgFiles=imgFiles;
		setBorder(BorderFactory.createLineBorder(java.awt.Color.green, 2));
		if(imgFiles!=null&imgFiles[si].exists()){
			img=Tools.getImg(imgFiles[si]);
			if(img!=null){
				imgWidth=img.getWidth();
				imgHeight=img.getHeight();
				rate=(float)img.getWidth()/img.getHeight();
			}
		}
		addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent e) {
				plm.playIndex(index);
				PS.controlBar.BrowseClickPlay();
			}

			public void mouseEntered(MouseEvent e) {
				mouseOver=true;
				if(aniAvail()){
					startChangeImg();
				}
			}

			public void mouseExited(MouseEvent e) {
				mouseOver=false;
				if(!aniAvail()){
					stopChangeImg();
				}
			}

			public void mousePressed(MouseEvent e) {}

			public void mouseReleased(MouseEvent e) {}
			
		});
		if(aniAvail()){
			startChangeImg();
		}
	}
	
	boolean aniAvail(){
		return isPlayIndex()|mouseOver;
	}
	
	boolean isPlayIndex(){
		return index==plm.getPlayIndex();
	}
	
	void startChangeImg(){
		if(!threadRun){
			imgChangeThread=new Thread(){
				public void run(){
					int select=0;
					threadRun=true;
					while(true){
						if(!aniAvail()){
							break;
						}
						if(select<imgFiles.length+1){
							setImg(imgFiles[select]);
							select++;
						}
						if(select>imgFiles.length-1){
							select=0;
						}
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							setImg(imgFiles[si]);
							break;
						}
					}
					threadRun=false;
				}
			};
			imgChangeThread.start();
		}
		
	}
	
	void refresh(){
		setImg(imgFiles[si]);
	}
	
	void stopChangeImg(){
		imgChangeThread.interrupt();
	}
	
	void setImg(File imgFile){
		img=Tools.getImg(imgFile);
		repaint();
	}
	
	public void paintComponent(Graphics g){
		Graphics2D g2=(Graphics2D)g;
		if(img!=null){
			imgWidth=img.getWidth();
			imgHeight=img.getHeight();
			rate=(float)img.getWidth()/img.getHeight();
		}
		Container cp=this;
		ch=cp.getBounds().height;
		cw=ch*rate ;
		cx=(cp.getBounds().width-cw)/2;
		if (cw > cp.getBounds().width) {
			cx = 0;
			cw = cp.getBounds().width;
			ch = cw /rate ;
		}
		cy = (cp.getBounds().height - ch) / 2;
		if(img!=null){
			g2.drawImage(img, (int) cx, (int) cy, (int) cw, (int) ch, this);
		}
	}
	
	public void paint(Graphics g){
		paintComponent(g);
		paintComponents(g);
		
		Point mousePoint = MouseInfo.getPointerInfo().getLocation();
		SwingUtilities.convertPointFromScreen(mousePoint, this);
		if(mousePoint.getX()>=0&mousePoint.getX()<getWidth()
				&mousePoint.getY()>=0&mousePoint.getY()<getHeight()){
			
		}
		
		if(mouseOver){
			Border bd=BorderFactory.createLineBorder(java.awt.Color.cyan , 2);
			bd.paintBorder(this, g, (int)cx, (int)cy,(int)cw, (int)ch);
		}else{
			Border bd=BorderFactory.createLineBorder(java.awt.Color.blue , 1);
			bd.paintBorder(this, g, (int)cx, (int)cy,(int)cw, (int)ch);
		}
		
		if(isPlayIndex()){
			Border bd=BorderFactory.createLineBorder(java.awt.Color.green , 3);
			bd.paintBorder(this, g, 0, 0, getWidth(), getHeight());
		}
	}
	
}